package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.entity.*;
import com.lanou3g.ptp.organize.service.ClazzService;
import com.lanou3g.ptp.organize.service.CollegeService;
import com.lanou3g.ptp.organize.service.MajorService;
import com.lanou3g.ptp.organize.service.SchoolService;
import com.lanou3g.ptp.user.service.UserService;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import com.lanou3g.ptp.vo.OrgNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "组织接口")
@RestController
public class OrganizeController {
    @Reference
    private CollegeService collegeService;
    @Reference
    private MajorService majorService;
    @Reference
    private ClazzService clazzService;
    @Reference
    private SchoolService schoolService;
    @Reference
    private UserService userService;

    @ApiOperation("获取某学校所有组织结构节点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId", value = "学校id")
    })
    @GetMapping("/school/{schoolId}/orgs")
    public PtpResult findOrgNodesBySchoolId(@PathVariable Long schoolId) {
        List<College> colleges = collegeService.findOrgNodesBySchoolId(schoolId);
        return PtpResultUtils.ok(colleges);
    }

    @ApiOperation("新增组织结构节点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level", value = "节点等级（1：学院；2：专业；3：班级）")
    })
    @PostMapping("/org/{level}")
    public PtpResult addNode(@PathVariable Integer level,
                             @RequestBody OrgNode node) {
        if (level == 1) {
            College college = new College();
            college.setName(node.getName());
            college.setSchoolId(node.getParentId());
            college.setLeaderId(node.getLeaderId());
            collegeService.addCollege(college);
        } else if (level == 2) {
            Major major = new Major();
            major.setName(node.getName());
            major.setCollegeId(node.getParentId());
            major.setLeaderId(node.getLeaderId());
            majorService.addMajor(major);
        } else {
            return PtpResultUtils.error(401, "level值错误");
        }
        return PtpResultUtils.ok();
    }

    @ApiOperation("修改组织结构节点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level", value = "节点等级（1：学院；2：专业；3：班级）")
    })
    @PutMapping("/org/{level}")
    public PtpResult updateNode(@PathVariable Integer level,
                                @RequestBody OrgNode node) {
        if (level == 1) {
            College college = new College();
            college.setId(node.getId());
            college.setName(node.getName());
            college.setSchoolId(node.getParentId());
            college.setLeaderId(node.getLeaderId());
            collegeService.updateById(college);
        }
        if (level == 2) {
            Major major = new Major();
            major.setId(node.getId());
            major.setName(node.getName());
            major.setCollegeId(node.getParentId());
            major.setLeaderId(node.getLeaderId());
            majorService.updateById(major);
        }
        return PtpResultUtils.ok();
    }

    @ApiOperation("删除组织结构节点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level", value = "节点等级（1：学院；2：专业；3：班级）")
    })
    @DeleteMapping("/org/{level}")
    public PtpResult deleteNode(@PathVariable Integer level,
                                @RequestBody OrgNode node) {
        if (level == 1) {
            collegeService.deleteById(node.getId());
        }
        if (level == 2) {
            majorService.deleteById(node.getId());
        }
        if (level == 3) {
            clazzService.deleteById(node.getId());
        }
        return PtpResultUtils.ok();
    }

    @ApiOperation("获取组织结构节点负责人候选列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level", value = "节点等级（1：学院；2：专业；3：班级）", required = true),
            @ApiImplicitParam(name = "id", value = "节点ID", required = true)
    })
    @GetMapping("/org/{level}/{id}/leaders")
    public PtpResult getNodeLeaders(@PathVariable Integer level,
                                    @PathVariable Long id) {
        List<User> users = null;
        if (level == 1) {
            users = userService.findByCollegeId(id);
        }
        if (level == 2 || level == 3) {
            users = userService.findByMajorId(id);
        }
        return PtpResultUtils.ok(users);
    }

    @ApiOperation("查询所有班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "显示条数"),
            @ApiImplicitParam(name = "majorId", value = "专业ID"),
            @ApiImplicitParam(name = "grade", value = "年级")
    })
    @GetMapping("/clazzes")
    public PtpResult findAll(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Long majorId, String grade) {
        PageInfo<Clazz> info = clazzService.findByPage(page, size,majorId,grade);
        return PtpResultUtils.ok(info);
    }

    @ApiOperation("添加班级")
    @PostMapping("/clazz")
    public PtpResult addCollege(@RequestBody Clazz clazz) {
        clazzService.addClazz(clazz);
        return PtpResultUtils.ok();
    }

    @ApiOperation("获取某学校所有学院")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId", value = "学校id")
    })
    @GetMapping("/school/{schoolId}/colleges")
    public PtpResult findCollegesBySchoolId(@PathVariable Long schoolId) {
        List<College> colleges = collegeService.findBySchoolId(schoolId);
        return PtpResultUtils.ok(colleges);
    }

    @ApiOperation("获取某学院所有专业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学院id")
    })
    @GetMapping("/college/{collegeId}/majors")
    public PtpResult findMajorsByCollegeId(@PathVariable Long collegeId) {
        List<Major> majors = majorService.findByCollegeIdSimple(collegeId);
        return PtpResultUtils.ok(majors);
    }

//    @ApiOperation("获取某专业所有班级")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "majorId", value = "专业id")
//    })
//    @GetMapping("/major/{majorId}/clazzes")
//    public PtpResult findClazzesByMajorId(@PathVariable Long majorId) {
//        List<Major> majors = clazzService.findByMajorIdSimple(majorId);
//        return PtpResultUtils.ok(majors);
//    }

    @ApiOperation("通过id批量删除班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "班级id")
    })
    @DeleteMapping("clazzes/{ids}")
    public PtpResult deleteByIds(@PathVariable Long[] ids) {
        clazzService.deleteByIds(ids);
        return PtpResultUtils.ok();
    }

    @ApiOperation("通过id修改班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "班级id")
    })
    @PutMapping("/clazz")
    public PtpResult updateById(@RequestBody Clazz clazz) {
        clazzService.updateById(clazz);
        return PtpResultUtils.ok();
    }

    @ApiOperation("通过id删除班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "班级id")
    })
    @DeleteMapping("clazz/{id}")
    public PtpResult deleteById(@PathVariable Long id) {
        clazzService.deleteById(id);
        return PtpResultUtils.ok();
    }

    @ApiOperation("通过ID获取学校信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学校id")
    })
    @GetMapping("/school/{id}")
    public PtpResult findSchoolBySchoolId(@PathVariable Long id) {
        School school = schoolService.findById(id);

        return PtpResultUtils.ok(school);
    }

    @ApiOperation("通过id修改学校")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学校id")
    })
    @PutMapping("/school/{id}")
    public PtpResult updateSchoolById(@PathVariable Long id, @RequestBody School school) {
        schoolService.updateById(school);
        return PtpResultUtils.ok();
    }

}
